package botlab.control;

import botlab.Game;
import com.jme3.bullet.BulletAppState;
import com.jme3.bullet.collision.PhysicsCollisionEvent;
import com.jme3.bullet.collision.PhysicsCollisionListener;
import com.jme3.bullet.control.GhostControl;
import com.jme3.scene.Spatial;
import com.jme3.scene.control.Control;

/**
 *
 * @author Quentin
 */
public class ShootableControl extends BaseControl implements PhysicsCollisionListener
{
    private int i;

    public ShootableControl()
    {
        Game.getApp().getStateManager().getState(BulletAppState.class).getPhysicsSpace().addCollisionListener(
                this);
    }

    public void collision(PhysicsCollisionEvent event)
    {   
        if (event.getNodeA().getName().equals(spatial.getName()))
        {
            final Spatial s = event.getNodeB();
            final String node = s.getName();
            
            if(s.getControl(GhostControl.class)!=null && s.getControl(GhostControl.class).isEnabled())
            {
                i++;
                s.getControl(GhostControl.class).setEnabled(false);
                Game.getApp().getRootNode().detachChild(s);
                Game.getText().setText("collision with : "+node+Integer.toString(i));
            }
            
        }
        
        else if (event.getNodeB().getName().equals(spatial.getName()))
        {
            final Spatial s = event.getNodeA();
            final String node = s.getName();
            if(s.getControl(GhostControl.class)!=null && s.getControl(GhostControl.class).isEnabled())
            {
                i++;
                s.getControl(GhostControl.class).setEnabled(false);
                Game.getApp().getRootNode().detachChild(s);
                Game.getText().setText("collision with : "+node+Integer.toString(i));
            }
  
        }
    }

    @Override
    protected void controlUpdate(float tpf)
    {
//        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public Control cloneForSpatial(Spatial spatial)
    {
        throw new UnsupportedOperationException("Not supported yet.");
    }

}
